package com.lzx.iteam;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Debug;
import android.webkit.CookieSyncManager;
import cn.jpush.android.api.JPushInterface;
import com.lzx.iteam.bean.SkinVertyData;
import com.lzx.iteam.skin.GetSkinResource;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.WebkitCookieManagerProxy;
import io.rong.imageloader.cache.disc.impl.UnlimitedDiskCache;
import io.rong.imageloader.cache.disc.naming.Md5FileNameGenerator;
import io.rong.imageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import io.rong.imageloader.core.DisplayImageOptions;
import io.rong.imageloader.core.ImageLoaderConfiguration;
import io.rong.imageloader.core.assist.QueueProcessingType;
import io.rong.imageloader.core.download.BaseImageDownloader;
import io.rong.imageloader.utils.StorageUtils;
import com.qiniu.android.storage.UploadManager;
import io.rong.imageloader.core.ImageLoader;
import io.rong.imkit.RongIM;
import io.rong.push.RongPushClient;

import java.io.File;
import java.net.CookieHandler;
import java.util.Map;

public class DialerApp extends Application {

    public long mYaoyiyaoShareContactId = -1;
    public boolean mYaoyiyaoShare = false;
    public boolean isGetChatGroupList = true;//是否执行获取小组列表接口
    public String mEventToWhere = "1";//事件选人结束，跳转哪个页面1：表示跳转到创建模板页面 2：表示跳转到模板编辑页面
    private volatile static UploadManager uploadManagerInstance;
    private PreferenceUtil mPreferenceUtil;
    public static Context mContext;
    // 用于存放倒计时时间
    public static Map<String, Long> map;
    public File cacheDir = null;

    @Override
    public void onCreate() {
        super.onCreate();
        CookieSyncManager.createInstance(this.getApplicationContext());
        CookieHandler.setDefault(new WebkitCookieManagerProxy());
        if (CloudDialerActivity.STARTUP_TRACE) Debug.startMethodTracing("dialer_app.trace");
        if (CloudDialerActivity.STARTUP_TRACE) Debug.stopMethodTracing();
        cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "Yun/Images/Cache");
        initImageLoader(getApplicationContext());
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        mContext = this.getApplicationContext();
        mPreferenceUtil = PreferenceUtil.getInstance(mContext);

        String path = mPreferenceUtil.getString(PreferenceUtil.CONTACT_KEYBOARD_PACKAGENAME, "local");
        boolean isT9 = mPreferenceUtil.getBoolean(PreferenceUtil.IS_T9, true);
        GetSkinResource getSkinResource = new GetSkinResource(this.getApplicationContext());
        if (CloudDialerActivity.mVertySkin == null) {
            CloudDialerActivity.mVertySkin = new SkinVertyData();
            CloudDialerActivity.mVertySkin = getSkinResource.getVertT9SkinData(CloudDialerActivity.mVertySkin, this, path);
        }

        if (CloudDialerActivity.mVertySkin2 == null) {
            CloudDialerActivity.mVertySkin2 = new SkinVertyData();
            CloudDialerActivity.mVertySkin2 = getSkinResource.getVertABCSkinData(CloudDialerActivity.mVertySkin2, this, path);
        }

        RongPushClient.registerHWPush(this);
        RongPushClient.registerMiPush(this, "2882303761517395685", "5271739536685");
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {
            RongIM.init(this);
            SealAppContext.init(this);
        }

    }

    public void initImageLoader(Context context) {

        ImageLoaderConfiguration config = new ImageLoaderConfiguration
              .Builder(context)
              .memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽
              //.discCacheExtraOptions(480, 800, Bitmap.CompressFormat.JPEG, 50, null) // Can slow ImageLoader, use it carefully (Better don't use it)/设置缓存的详细信息，最好不要设置这个
              // TODO 设置这个属性会使，每次加载图片加载两遍 ^
              .threadPoolSize(5)//线程池内加载的数量
              .threadPriority(Thread.NORM_PRIORITY - 1)
              .denyCacheImageMultipleSizesInMemory()
              .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
              //.memoryCacheSize(2 * 1024 * 1024)
              .discCacheSize(50 * 1024 * 1024)
              .discCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密
              .tasksProcessingOrder(QueueProcessingType.LIFO)
              //.discCacheFileCount(100) //缓存的文件数量
              //.discCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
              .discCache(new UnlimitedDiskCache(cacheDir))
              .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
              .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
              .writeDebugLogs() // Remove for release app
              .build();//开始构建
        ImageLoader.getInstance().init(config);
    }

    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
              .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
              .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    public static UploadManager getUploadManager(Context context) {
        if (uploadManagerInstance == null) {
            uploadManagerInstance = new UploadManager();
        }

        return uploadManagerInstance;
    }


    public boolean isGetChatGroupList() {
        return isGetChatGroupList;
    }

    public void setGetChatGroupList(boolean isGetChatGroupList) {
        this.isGetChatGroupList = isGetChatGroupList;
    }

    public String getmEventToWhere() {
        return mEventToWhere;
    }

    public void setmEventToWhere(String mEventToWhere) {
        this.mEventToWhere = mEventToWhere;
    }
}
